<?php
namespace App\Api\Controllers;

use App\Api\Models\CmcBankCard;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use App\Api\Services\JsonService;
use App\Api\Services\BankCard;
use App\Api\Manages\CommonController;

class CmcBankCardController extends Controller
{
    /**
     * 查询用户绑定银行卡
     * @param Request $request
     * @param $memberId 用户id
     * @return array
     */
    public function bankCard(Request $request)
    {
        try {
            $params = $this->validate($request,[
                'member_id'=>'required|integer'
            ]);
        }catch(ValidationException $validationException){
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        $memberId = (int)$params['member_id'];

        $allowField = ['bank_card_number','bank_name'];

        $bankCard = CmcBankCard::getBankCard($memberId,$allowField);

        $count = count($bankCard);

        //查询到的银行卡号进行加密
        for($i=0;$i<$count;$i++){
            $bankCard[$i]['bank_card_number'] = substr_replace($bankCard[$i]['bank_card_number'],'**** **** **** ',0,-4);
        }

        if($bankCard){
            return JsonService::success($bankCard);
        }
        return JsonService::fail('未绑定银行卡','',1003);
    }

    /**
     * 用户银行卡绑定
     * @param Request $request
     * @return array
     */
    public function bindBankCard(Request $request)
    {
        try {
            $params = $this->validate($request,[
                'member_id'=>'required|integer',
                'cardholder_name'=>'required|max:30',
                'id_card'=>'required|alpha_num|max:18',
                'idcard_start_time'=>'required|integer',
                'idcard_end_time'=>'required|integer',
                'bank_card_number'=>'required|max:19',
                'bank_name'=>'required',
                'phone'=>'required|integer'
            ]);
        }catch (ValidationException $validationException){
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        //验证银行卡
        $authentication = json_decode(BankCard::bankCardAuthentication($params['bank_card_number'],$params['id_card'],$params['cardholder_name'],$params['phone']),true);

        if($authentication['data']['result'] == 2){
            return JsonService::fail('银行卡验证失败');
        }
        return JsonService::success('银行卡验证成功');
    }

    /**
     * 银行卡绑定手机验证码发送接口
     * @param Request $request
     * @return array
     */
    public function messagePin(Request $request)
    {
        try {
            $params = $this->validate($request,[
                'phone'=>'required|integer'
            ]);
        }catch (ValidationException $validationException){
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        $number = CommonController::makeRandomNumber();

        return CommonController::sendPhoneCode($params['phone'],$number);
    }

    /**
     * 进行数据的添加
     * @param Request $request
     * @return array
     */
    public function insBankCard(Request $request)
    {
        try {
            $params = $this->validate($request,[
                'member_id'=>'required|integer',
                'cardholder_name'=>'required|max:30',
                'id_card'=>'required|alpha_num|max:18',
                'idcard_start_time'=>'required|integer',
                'idcard_end_time'=>'required|integer',
                'bank_card_number'=>'required|max:19',
                'bank_name'=>'required',
                'phone'=>'required|integer'
            ]);
        }catch (ValidationException $validationException){
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        return CmcBankCard::addBankCard($params);
    }
}